home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / program / funnel.zoo / sources / lister.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-11  |  11.9 KB  |  369 lines

  1. /*##############################################################################
  2.  
  3. FUNNNELWEB COPYRIGHT
  4. ====================
  5. FunnelWeb is a literate-programming macro preprocessor.
  6.  
  7. Copyright (C) 1992 Ross N. Williams.
  8.  
  9.    Ross N. Williams
  10.    ross@spam.adelaide.edu.au
  11.    16 Lerwick Avenue, Hazelwood Park 5066, Australia.
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of Version 2 of the GNU General Public License as
  15. published by the Free Software Foundation.
  16.  
  17. This program is distributed WITHOUT ANY WARRANTY; without even the implied
  18. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. See Version 2 of the GNU General Public License for more details.
  20.  
  21. You should have received a copy of Version 2 of the GNU General Public
  22. License along with this program. If not, you can FTP the license from
  23. prep.ai.mit.edu/pub/gnu/COPYING-2 or write to the Free Software
  24. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. Section 2a of the license requires that all changes to this file be
  27. recorded prominently in this file. Please record all changes here.
  28.  
  29. Programmers:
  30.    RNW  Ross N. Williams  ross@spam.adelaide.edu.au
  31.  
  32. Changes:
  33.    07-May-1992  RNW  Program prepared for release under GNU GPL V2.
  34.  
  35. ##############################################################################*/
  36.  
  37.  
  38. /******************************************************************************/
  39. /*                                    LISTER.C                                */
  40. /******************************************************************************/
  41.  
  42. #include "style.h"
  43.  
  44. #include "as.h"
  45. #include "data.h"
  46. #include "list.h"
  47. #include "lister.h"
  48. #include "machin.h"
  49. #include "misc.h"
  50. #include "table.h"
  51. #include "writfile.h"
  52.  
  53. /******************************************************************************/
  54.  
  55. #define LISTWIDTH     80  /* Nominal width of listing file. Can be wider.     */
  56. #define INDENTQ        3  /* Indentation quantum for include files.           */
  57. #define MESSMAXCH    200  /* Maximum number of characters in a message.       */
  58. #define CTXINF       100  /* Special value that signals infinite context.     */
  59. #define MAXLINES   10000  /* Maximum number of lines in input file.           */
  60.  
  61. /******************************************************************************/
  62.  
  63. /* The following #defines and typedef define an enumerated type for message   */
  64. /* kinds. There are four kinds of message, and we need a type for storing     */
  65. /* this information. mess_k_t stands for message_kind_type.                   */
  66. /* We use #defines instead of enums as enums are not portable enough.         */
  67. #define MESS_MES 1
  68. #define MESS_WAR 2
  69. #define MESS_ERR 3
  70. #define MESS_SEV 4
  71. typedef ubyte mess_k_t;
  72.  
  73. /* We wish to store messages in a table with message positions as keys.       */
  74. /* However, the table package will not tolerate duplicate keys. To solve this */
  75. /* problem, we create a new type 'unqpos_t' (unique position type) which has  */
  76. /* not only a position but a serial number as well.                           */
  77. typedef struct
  78.   {
  79.    ps_t  up_pos;
  80.    ulong up_serial;
  81.   } unqpos_t;
  82.  
  83. /* The following structure stores a single message. As we are storing COPIES  */
  84. /* of the messages, we impose a maximum limit (MESSMAXCH) to their length.    */
  85. typedef struct
  86.   {
  87.    mess_k_t ms_kind;
  88.    char     ms_text[MESSMAXCH+1];
  89.   } mess_t;
  90.  
  91. /******************************************************************************/
  92.  
  93. LOCVAR p_tb_t p_msgtab;           /* The message table storing diagnostics.   */
  94. LOCVAR ulong  serial_next;        /* Serial number for numbering messages.    */
  95.  
  96. /******************************************************************************/
  97.  
  98. LOCAL int cmpuqpos P_((unqpos_t *,unqpos_t *));
  99. LOCAL int cmpuqpos(p1,p2)
  100. /* Compare two unique positions for the table package and returns [-1,0,1].   */
  101. unqpos_t *p1;
  102. unqpos_t *p2;
  103. {
  104.  long  diff;
  105.  ubyte i;
  106.  
  107.  for (i=1; i<=3; i++)
  108.    {
  109.     switch (i)
  110.       {
  111.        case 1: diff= p1->up_pos.ps_line   - p2->up_pos.ps_line;   break;
  112.        case 2: diff= p1->up_pos.ps_column - p2->up_pos.ps_column; break;
  113.        case 3: diff= p1->up_serial        - p2->up_serial;  break;
  114.        default: as_bomb("cmpuqpos: Case defaulted.");
  115.       }
  116.     if (diff<0)
  117.        return -1;
  118.     else
  119.        if (diff>0)
  120.           return 1;
  121.    }
  122.  return 0;
  123. }
  124.  
  125. /******************************************************************************/
  126.  
  127. /* Converts a value of message kind type into a representative string. */
  128. /* This routine unused at present.
  129. LOCAL char *mess_let P_((mess_k_t));
  130. LOCAL char *mess_let (mess_k)
  131. mess_k_t mess_k;
  132. {
  133.  switch (mess_k)
  134.    {
  135.     case MESS_MES : return "M";
  136.     case MESS_WAR : return "W";
  137.     case MESS_ERR : return "E";
  138.     case MESS_SEV : return "S";
  139.     default: as_bomb("mess_let: Case defaulted.");
  140.    }
  141.  as_bomb("mess_let: Switch dropped out.");
  142.  return "Failure";
  143. }
  144. */
  145.  
  146. /******************************************************************************/
  147.  
  148. #define DUPMAX 200
  149.  
  150. LOCAL char *dup P_((int,uword));
  151. LOCAL char *dup(ch,count)
  152. /* Returns a pointer to a STAVAR string containing 'count' copies of 'ch'.    */
  153. /* Count must be in the range [0,DUPMAX]. The pointer returned always points  */
  154. /* to the same address which is a 'STAVAR' inside this function.              */
  155. int   ch;
  156. uword count;
  157. {
  158.  STAVAR char buffer[DUPMAX+1];
  159.  
  160.  as_cold(count<DUPMAX,"lister.dup: count>=DUPMAX");
  161.  /* The IF in the following is just in case 'memset' is brain damaged. */
  162.  if (count>0) memset(&buffer[0],ch,(size_t) count);
  163.  buffer[count]=EOS;
  164.  return &buffer[0];
  165. }
  166.  
  167. /******************************************************************************/
  168.  
  169. LOCAL void mess_wri P_((p_wf_t,mess_t *,uword,uword));
  170. LOCAL void mess_wri(p_wf,p_mess,indent,column)
  171. /* Writes message p_mess to file p_wf indented by INDENTQ*indent+column.      */
  172. p_wf_t p_wf;
  173. mess_t *p_mess;
  174. uword  indent;
  175. uword  column;
  176. {
  177.  switch (p_mess->ms_kind)
  178.    {
  179.     case MESS_MES : wf_wr(p_wf,"            "); break;
  180.     case MESS_WAR : wf_wr(p_wf,"     Warning"); break;
  181.     case MESS_ERR : wf_wr(p_wf,"       Error"); break;
  182.     case MESS_SEV : wf_wr(p_wf,"      Severe"); break;
  183.     default: as_bomb("mess_let: Case defaulted.");
  184.    }
  185.  wf_wr(p_wf,"|.");
  186.  wf_wr(p_wf,dup('.',indent*INDENTQ));
  187.  wf_wr(p_wf,dup('.',column-1));
  188.  wf_wr(p_wf,"^");
  189.  wf_wr(p_wf,&p_mess->ms_text[0]);
  190.  wf_wr(p_wf,"\n");
  191. }
  192.  
  193. /******************************************************************************/
  194.  
  195. LOCAL void line_wri P_((p_wf_t,ln_t *));
  196. LOCAL void line_wri(p_wf,p_line)
  197. /* Writes the given line to the given output stream. */
  198. p_wf_t  p_wf;
  199. ln_t   *p_line;
  200. {
  201.  char buffer[100];
  202.  
  203.  /* The last line of the line list is the EOF marker line and to indicate     */
  204.  /* that it is not really part of the input file, we omit it's line number.   */
  205.  if (p_line->ln_global==ls_len(line_list))
  206.                  /*" 12345 12345| "*/
  207.     sprintf(buffer,"            | ");
  208.  else
  209.     sprintf(buffer," %5lu %5lu| ",
  210.                    (unsigned long) p_line->ln_global,
  211.                    (unsigned long) p_line->ln_local);
  212.  wf_wr(p_wf,buffer);
  213.  wf_blk(p_wf,p_line->ln_body.sc_first,(size_t)
  214.      (p_line->ln_body.sc_last-p_line->ln_body.sc_first+1));
  215. }
  216.  
  217. /******************************************************************************/
  218.  
  219. LOCAL void add_mess P_((ps_t *,mess_k_t,char *));
  220. LOCAL void add_mess(p_pos,messkind,p_string)
  221. /* Creates a message record and places the message information in the         */
  222. /* argument into the record. Inserts the record into the message table.       */
  223. ps_t      *p_pos;
  224. mess_k_t   messkind;
  225. char      *p_string;
  226. {
  227.  mess_t   tempmess;
  228.  unqpos_t unqpos;
  229.  
  230.  tempmess.ms_kind=messkind;
  231.  as_cold(strlen(p_string)<=MESSMAXCH,
  232.          "lister.add_mess: Parameter message is too long.");
  233.  strcpy(tempmess.ms_text,p_string);
  234.  ASSIGN(unqpos.up_pos,*p_pos);
  235.  unqpos.up_serial=serial_next++;
  236.  tb_ins(p_msgtab,PV &unqpos,PV &tempmess);
  237. }
  238.  
  239. /******************************************************************************/
  240.  
  241. EXPORT void lr_ini P_((void))
  242. {
  243.  /* Create a brand new empty message table. */
  244.  p_msgtab=tb_cre(sizeof(unqpos_t),sizeof(mess_t),(p_kycm_t) cmpuqpos);
  245.  
  246.  /* Start the serial numbers at zero again. */
  247.  serial_next=0;
  248. }
  249.  
  250. /******************************************************************************/
  251.  
  252. EXPORT void lr_mes(p_pos,s)
  253. ps_t *p_pos;
  254. char *s;
  255. {
  256.  add_mess(p_pos,MESS_MES,s);
  257. }
  258.  
  259. /******************************************************************************/
  260.  
  261. EXPORT void lr_war(p_pos,s)
  262. ps_t *p_pos;
  263. char *s;
  264. {
  265.  add_mess(p_pos,MESS_WAR,s);
  266.  num_war++;
  267. }
  268.  
  269. /******************************************************************************/
  270.  
  271. EXPORT void lr_err(p_pos,s)
  272. ps_t *p_pos;
  273. char *s;
  274. {
  275.  add_mess(p_pos,MESS_ERR,s);
  276.  num_err++;
  277. }
  278.  
  279. /******************************************************************************/
  280.  
  281. EXPORT void lr_sev(p_pos,s)
  282. ps_t *p_pos;
  283. char *s;
  284. {
  285.  add_mess(p_pos,MESS_SEV,s);
  286.  num_sev++;
  287. }
  288.  
  289. /******************************************************************************/
  290.  
  291. EXPORT void lr_gen(p_wf,ctx)
  292. p_wf_t p_wf;
  293. uword  ctx;
  294. {
  295.  unqpos_t next_pos; /* Position of next message.                              */
  296.  mess_t   next_mes; /* Content  of next message.                              */
  297.  long     prev_no;  /* Global line number of previous message.                */
  298.  long     next_no;  /* Global line number of next     message.                */
  299.  bool     ingap;    /* Controls issuance of gap markers in the listing.       */
  300.  long     context=ctx; /* Signed version of the parameter.                    */
  301.  
  302.  /* A context of CTXINF signals an infinite context. */
  303.  if (context==CTXINF) context=MAXLINES;
  304.  
  305.  /* Write listing header. */
  306.  wf_wr(p_wf,"\nGlobal Local| Input File\n");
  307.  wf_wr(p_wf,dup('-',12));wf_wr(p_wf,"+");
  308.  wf_wr(p_wf,dup('-',LISTWIDTH-13));wf_wr(p_wf,"\n");
  309.  
  310.  /* Reset the line list and message table for reading. */
  311.  ls_fir(line_list);
  312.  tb_fir(p_msgtab);
  313.  
  314.  /* Prime the variables ingap, prev_no, next_no, next_pos, and next_mes. */
  315.  ingap=FALSE; prev_no = -(context+1);
  316.  if (tb_rea(p_msgtab,PV &next_pos,PV &next_mes))
  317.     next_no=next_pos.up_pos.ps_line;
  318.  else
  319.     next_no=MAXLINES+context+1;
  320.  
  321.  while (TRUE)
  322.    {/* Process a single line per iteration. */
  323.     ln_t *p_line;
  324.  
  325.     /* Grab the next line from the line list. */
  326.     ls_nxt(line_list,PPV &p_line);
  327.     if (p_line==NULL) break;
  328.  
  329.     /* List the line if it is within context range of prev or next message. */
  330.     if ((((long) p_line->ln_global) <= prev_no+context) ||
  331.         (((long) p_line->ln_global) >= next_no-context))
  332.        {line_wri(p_wf,p_line);ingap=FALSE;}
  333.     else
  334.        if (!ingap)
  335.           {wf_wr(p_wf,".................\n");ingap=TRUE;}
  336.  
  337.     /* Issue any messages that are about this line. */
  338.     while (next_no == p_line->ln_global)
  339.       {
  340.        /* Write the current message. */
  341.        mess_wri(p_wf,&next_mes,(uword) 0,
  342.                 (uword) next_pos.up_pos.ps_column);
  343.                 
  344.        /* Read the next message from the table. */
  345.        prev_no=next_no;
  346.        if (tb_rea(p_msgtab,PV &next_pos,PV &next_mes))
  347.           next_no=next_pos.up_pos.ps_line;
  348.        else
  349.           next_no=MAXLINES+context+1;
  350.       }
  351.    } /* End while */
  352.  
  353.  /* Make sure that we have read to the end of the message table. */
  354.  {bool result;
  355.   result=tb_rea(p_msgtab,PV &next_pos,PV &next_mes);
  356.   as_cold(!result,
  357.           "lr_gen: Messages remaining in message table after listing.");
  358.  }
  359.  
  360.  /* Write listing trailer. */
  361.  wf_wr(p_wf,dup('-',12));wf_wr(p_wf,"+");
  362.  wf_wr(p_wf,dup('-',LISTWIDTH-13));wf_wr(p_wf,"\n");
  363.  wf_wl(p_wf,"");
  364. }
  365.  
  366. /******************************************************************************/
  367. /*                               End of LISTER.C                              */
  368. /******************************************************************************/
  369.